var gridCDS;

function createGrid() { 
	var producerFormatter = function(value, rowIdx){
        return value.name;
    };
    var typeCdFormatter = function(value, rowIdx){
        return value.type;
    };
    var dateFormatter = function(value, rowIdx) {
    	var date = new Date(value);
    	return date.getDate()+" "+MONTHS[date.getMonth()]+" "+date.getFullYear();
    };
	
	require([
	         "dojox/grid/DataGrid",
	         "dojo/store/Memory",
	         "dojo/data/ObjectStore",
	         "dojo/_base/xhr",
	         "dojo/domReady!"
	     ], function(DataGrid, Memory, ObjectStore, xhr){
	         var dataStore;
	         xhr.get({
	             url: "retriveData?operation=getCds",
	             handleAs: "json"
	         }).then(function(data){
	             dataStore =  new ObjectStore({ objectStore:new Memory({ data: data.items }) });
	      
	             // set the layout structure:
	             var columns = [{
	                 field: "cdId",
	                 name: "Nr.",
	                 width: '50px'
	             },
	             {
	                 field: "name",
	                 name: 'Name',
	                 width: 'auto'
	             },
	             {
	                 field: 'producer',
	                 name: 'Producer',
	                 width: '200px',
	                 formatter: producerFormatter
	             },
	             {
	                 field: 'cdType',
	                 name: 'Type',
	                 width: '100px',
	                 formatter: typeCdFormatter
	             },
	             {
	                 field: 'acquisitionDate',
	                 name: 'Acquisition Date',
	                 width: '200px',
	                 formatter: dateFormatter
	             }
	             ];
	             
	             /*create a new grid:*/ 
	             gridCDS = new dojox.grid.DataGrid({ 
	                 store: dataStore,
	                 structure: columns,
	                 clientSort: false,
	                 rowSelector: '5px',
	                 onRowDblClick : editCD}, 
	               document.createElement('div')); 
	             //append the new grid to the div 
	             dojo.byId("gridDiv").appendChild(gridCDS.domNode);
	             dojo.connect(gridCDS, "onKeyUp", function(event) {
	            	if (event.keyCode == 46) { // Delete item
	            		removeCD();
	            	}
	            });
	            // Call startup() to render the grid 
	             gridCDS.startup(); 
	        });
	    });
	}
